// pages/cart/cart.js
var http = require("../../utils/http.js");
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts: [],
    cartAccount: 0,
    selectAllChecked: false,
    selectLength: 0
  },
  addProductNumber(e) {
    let index = e.target.dataset.index;
    this.data.carts[index].quantity++;
    this.setData({
      carts: this.data.carts
    });
    this.initData();
  },
  subProductNumber(e) {
    let index = e.target.dataset.index;
    this.data.carts[index].quantity--;
    let _this = this;
    if (this.data.carts[index].quantity <= 0) {
      wx.showModal({
        content: '确定将此商品移除购物车?',
        success(res) {
          if (res.confirm) {
            _this.data.carts.splice(index, 1);

            app.globalData.cartCount--;
          } else if (res.cancel) {
            _this.data.carts[index].quantity = 1;

          }
          _this.setData({
            carts: _this.data.carts
          });
          _this.initData();
        }
      })
    } else {
      _this.setData({
        carts: _this.data.carts
      });
      this.initData();
    }

  },
  setSelectLength() {
    let num = 0;
    for (let i = 0; i < this.data.carts.length; i++) {
      if (this.data.carts[i].checked) {
        num++;
      }
    }
    this.setData({
      selectLength: num
    });
  },
  selectAll: function(e) {
    if (e.detail.value.length == 0) {
      this.setData({
        selectAllChecked: false
      });
      this.setAll(false);
    } else {
      this.setData({
        selectAllChecked: true
      });

      this.setAll(true);

    }
    this.setSelectLength();
  },
  setAll(val) {
    for (let i = 0; i < this.data.carts.length; i++) {
      this.data.carts[i].checked = val;
    }
    this.setData({
      carts: this.data.carts
    });
    this.computedSum();
  },
  productSelected: function(e) {
    let index = e.target.dataset.index;
    if (e.detail.value.length == 0) {
      this.data.carts[index].checked = false;
    } else {
      this.data.carts[index].checked = true;
    }
    this.isSelectedAll();
    this.setSelectLength();
    this.computedSum();
  },
  isSelectedAll() {
    let isSelectedAll = true;

    for (let i = 0; i < this.data.carts.length; i++) {
      if (!this.data.carts[i].checked) {
        isSelectedAll = false;
        break;
      }
    }

    this.setData({
      selectAllChecked: isSelectedAll
    });
  },
  computedSum() {
    let sum = 0;
    this.data.carts.forEach(item => {
      if (item.checked) {
        sum += item.price * item.quantity;
      }
    });
    this.setData({
      cartAccount: sum
    })
    return sum;
  },
  setTabBarBadge() {

    let app = getApp();

    if (app.globalData.cartCount != 0) {
      wx.setTabBarBadge({
        index: 2,
        text: app.globalData.cartCount.toString(),
      })
    } else {
      wx.removeTabBarBadge({
        index: 2
      })
    }

  },
  initData() {
    this.computedSum();
    this.isSelectedAll();
    this.setSelectLength();
    this.setTabBarBadge();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  loadData() {
    let params = {
      model: 'wechatshop.cart',
      field: "user_id,product_id,quantity,price,account,checked",
      domain: JSON.stringify([
        ['user_id', '=', wx.getStorageSync('is_login')]
      ])
    };
    http.request('/wechatshop/api/object/get', params).then(data => {
      console.log(data);
      for (let i = 0; i < data.length; i++) {
        data[i].image_url = http.requsetImageUrl('wechatshop.product', 'image_url', data[i].product_id[0]);
      }
      this.setData({
        carts: data
      });
      let app = getApp();
      app.globalData.cartCount = data.length;
      this.initData();
    })
  },
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})